<?
    /* -*- Mode: PHP5; tab-width: 4; indent-tabs-mode: nil; basic-offset: 4 -*- */

    /*********************************************************
    *            --== InArch ==--
    *
    * @author Kulikov Alexey <a.kulikov@gmail.com>
    * @version n/a
    * @since 07.06.2005
    * @copyright essentialmind.com 2005
    *
    *********************************************************/
    
    class pager{
        
        /**
         * Total number of records
         *
         * @var integer
         */
        private $total;
        
        /**
         * Records per page
         *
         * @var int
         */
        private $perPage;
        
        /**
         * Current Page
         *
         * @var int
         */
        private $currentPage;
        
        
        /**
         * Allow pagers to slide? (not take up the whole screen)
         *
         * @var bool
         */
        private $slide = true;
        
        
        /**
         * What is the default "slide" value (number of items per slide)
         *
         * @var int
         */
        private $slideAmmount = 11;
        
        /* --------- END VARIABLE DECLARATION ----------- */
        
        
        /**
         * Constructor
         *
         * @param integer $total        total number of records
         * @param integer $perPage      records per page
         * @param integer $currentPage  current page
         */
        public function __construct($total,$perPage=10,$currentPage=1){
            $this->total        = (int)$total;
            $this->perPage      = (int)$perPage;
            $this->currentPage  = (int)$currentPage;
        }
        
        
        /**
         * Returns an array that represents the pager with indexes ID
         * and 'current' set to true in case the current element corresponds
         * to the selected page
         *
         * @return array
         */
        public function getPager(){
            $pages = array();
            $totalPages = ceil($this->total / $this->perPage);
            
            //is there a need for a pager at all?
            if($totalPages == 1){
                return false;   
            }
            
            //process pager
            for($i = 1; $i <= $totalPages; $i++){
                $pages[$i] = array('id'=>$i,'name'=>$i);
            }
            
            //check for errors
            if($totalPages >= $this->currentPage){
                $pages[$this->currentPage]['current'] = true;
            }
            
            //now, think about sliding!
            if($this->slide and count($pages) > $this->slideAmmount){
                $slider     = floor($this->slideAmmount / 2);
                
                //left boundary slide fix
                if($this->currentPage <= $slider){
                    $slideLeft  = $this->currentPage - $slider - 1; 
                    $slideRight = $slider - $this->currentPage + 1;
                }
                
                //right boundary fix
                if(($this->currentPage + $slider) > $totalPages){
                    $slideLeft  = ($this->currentPage + $slider) - $totalPages; 
                    $slideRight = $totalPages - ($this->currentPage + $slider);
                }
                
                $slideLeft  = $slideLeft + $slider;
                $slideRight = $slideRight + $slider;
                
                foreach($pages as $key => $page){
                    if($key < ($this->currentPage - $slideLeft) or $key > ($this->currentPage + $slideRight)){
                        unset($pages[$key]);
                    }
                }
                
                //do we stick a link to the beginning?
                if($slideLeft >= $slider and $this->currentPage > ($slider + 1)){
                    array_unshift($pages,array('id'=>'1','name'=>'&lt;&lt;'));   
                }
                
                //do we stick a link to the end?
                if($slideRight >= $slider and $this->currentPage < ($totalPages - $slider)){
                    array_push($pages,array('id'=>$totalPages,'name'=>'&gt;&gt;'));   
                }
                
            }
            
            //return data to the requesting object
            return $pages;
        }
          
    }
?>